package com.ljb.main.v;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.VideoView;

import com.hikvision.vmsnetsdk.CameraInfo;
import com.ljb.haikanglibrary.ControlModel;
import com.ljb.haikanglibrary.HaiKangManager;
import com.ljb.haikanglibrary.live.ConstantLive;
import com.ljb.haikanglibrary.live.LiveCallBack;
import com.ljb.haikanglibrary.utils.L;
import com.ljb.haikanglibrary.utils.ThreadUtils;
import com.ljb.main.DataTemp;
import com.ljb.main.R;
import com.ljb.main.ui.LoadStateLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljb on 2017/5/26.
 */

public class LiveFragment extends Fragment implements View.OnClickListener {

    private int position;
    private List<CameraInfo> cameraInfoList = new ArrayList<>();
    private List<TextView> textViewList = new ArrayList<>();
    private List<VideoView> videoViewList = new ArrayList<>();
    private List<LoadStateLayout> loadStateLayoutList = new ArrayList<>();
    private List<ControlModel> controlModelList = new ArrayList<>();

    public static LiveFragment newInstance(int position) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        LiveFragment fragment = new LiveFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.e("LiveFragment开始加载");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        L.e("LiveFragment加载成功");
        View rootView = inflater.inflate(R.layout.fragment_live, container, false);
        initUI(rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        position = getArguments().getInt("position");
        cameraInfoList = DataTemp.init().cameraInfoList_region;
    }

    private void initUI(View rootView) {
        textViewList.clear();
        videoViewList.clear();
        loadStateLayoutList.clear();

        TextView tv1 = (TextView) rootView.findViewById(R.id.tv1);
        VideoView vv1 = (VideoView) rootView.findViewById(R.id.vv1);
        TextView tv2 = (TextView) rootView.findViewById(R.id.tv2);
        VideoView vv2 = (VideoView) rootView.findViewById(R.id.vv2);
        TextView tv3 = (TextView) rootView.findViewById(R.id.tv3);
        VideoView vv3 = (VideoView) rootView.findViewById(R.id.vv3);
        TextView tv4 = (TextView) rootView.findViewById(R.id.tv4);
        VideoView vv4 = (VideoView) rootView.findViewById(R.id.vv4);
        TextView tv5 = (TextView) rootView.findViewById(R.id.tv5);
        VideoView vv5 = (VideoView) rootView.findViewById(R.id.vv5);
        TextView tv6 = (TextView) rootView.findViewById(R.id.tv6);
        VideoView vv6 = (VideoView) rootView.findViewById(R.id.vv6);
        LoadStateLayout stateLayout1 = (LoadStateLayout) rootView.findViewById(R.id.stateLayout1);
        LoadStateLayout stateLayout2 = (LoadStateLayout) rootView.findViewById(R.id.stateLayout2);
        LoadStateLayout stateLayout3 = (LoadStateLayout) rootView.findViewById(R.id.stateLayout3);
        LoadStateLayout stateLayout4 = (LoadStateLayout) rootView.findViewById(R.id.stateLayout4);
        LoadStateLayout stateLayout5 = (LoadStateLayout) rootView.findViewById(R.id.stateLayout5);
        LoadStateLayout stateLayout6 = (LoadStateLayout) rootView.findViewById(R.id.stateLayout6);
        textViewList.add(tv1);
        textViewList.add(tv2);
        textViewList.add(tv3);
        textViewList.add(tv4);
        textViewList.add(tv5);
        textViewList.add(tv6);
        videoViewList.add(vv1);
        videoViewList.add(vv2);
        videoViewList.add(vv3);
        videoViewList.add(vv4);
        videoViewList.add(vv5);
        videoViewList.add(vv6);
        loadStateLayoutList.add(stateLayout1);
        loadStateLayoutList.add(stateLayout2);
        loadStateLayoutList.add(stateLayout3);
        loadStateLayoutList.add(stateLayout4);
        loadStateLayoutList.add(stateLayout5);
        loadStateLayoutList.add(stateLayout6);

        for (LoadStateLayout layout : loadStateLayoutList) {
            layout.setErrorView(R.layout.layout_load_error);
            layout.setLoadingView(R.layout.layout_loading);
            layout.setEmptyView(R.layout.layout_load_empty);

            layout.setState(LoadStateLayout.STATE_EMPTY);
        }
    }

    @Override
    public void onPause() {
        L.e("onPause");
        for (ControlModel controlModel : controlModelList) {
            if (controlModel != null) {
                HaiKangManager.init().stopLive(controlModel);
            }
        }
        controlModelList.clear();
        super.onPause();
    }


    @Override
    public void onResume() {
        super.onResume();
        L.e("onResume");
        initData();
    }

    private void initData() {
        int size = cameraInfoList.size();
        int count = size - position * 6;

        if (count >= 6) {
            for (int i = 0; i < 6; i++) {
                int currentPosition = 6 * position + i;
                CameraInfo cameraInfo = cameraInfoList.get(currentPosition);
                textViewList.get(i).setText(cameraInfo.name);
//                loadStateLayoutList.get(i).setState(LoadStateLayout.STATE_LOADING);
//                loadStateLayoutList.get(i).setState(LoadStateLayout.STATE_SUCCESS);
                requestData(cameraInfo, videoViewList.get(i), i);
                loadStateLayoutList.get(i).setOnClickListener(this);
            }

        } else {
            for (int i = 0; i < count; i++) {
                int currentPosition = 6 * position + i;
                CameraInfo cameraInfo = cameraInfoList.get(currentPosition);
                textViewList.get(i).setText(cameraInfo.name);
//                loadStateLayoutList.get(i).setState(LoadStateLayout.STATE_LOADING);
//                loadStateLayoutList.get(i).setState(LoadStateLayout.STATE_SUCCESS);
                requestData(cameraInfo, videoViewList.get(i), i);
                loadStateLayoutList.get(i).setOnClickListener(this);
            }
        }
    }


    private void requestData(final CameraInfo cameraInfo, final VideoView vv, final int position) {
        loadStateLayoutList.get(position).setState(LoadStateLayout.STATE_LOADING);
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                ControlModel controlModel = HaiKangManager.init().requestDeviceInfo(cameraInfo, new LiveCallBack() {
                    @Override
                    public void onMessageCallback(final int message) {
                        L.e("当前位置:" + position);
                        HaiKangManager.init().showMessage(message);
                        ThreadUtils.runUIThread(new Runnable() {
                            @Override
                            public void run() {
                                if (message == ConstantLive.PLAY_DISPLAY_SUCCESS) {
                                    loadStateLayoutList.get(position).setState(LoadStateLayout.STATE_SUCCESS);
                                } else if (message == ConstantLive.RTSP_FAIL) {
                                    loadStateLayoutList.get(position).setState(LoadStateLayout.STATE_ERROR);
                                } else if (message == ConstantLive.GET_PLAYURL_ERROR) {
                                    loadStateLayoutList.get(position).setState(LoadStateLayout.STATE_ERROR);
                                } else if (message == ConstantLive.START_OPEN_FAILED) {
                                    loadStateLayoutList.get(position).setState(LoadStateLayout.STATE_ERROR);
                                }
                            }
                        });
                    }
                });

                controlModelList.add(controlModel);
                vv.setTag(controlModel);
                String cameraID = cameraInfo.cameraID;
                if (TextUtils.equals(cameraID, "160") || TextUtils.equals(cameraID, "179")) {//这两台设备没有MAG码流,所以直接加载子码流
                    HaiKangManager.init().startLive(controlModel, 1, vv);
                } else {
                    HaiKangManager.init().startLive(controlModel, 2, vv);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        int p = -1;
        switch (v.getId()) {
            case R.id.stateLayout1:
                p = 0;
                break;
            case R.id.stateLayout2:
                p = 1;
                break;
            case R.id.stateLayout3:
                p = 2;
                break;
            case R.id.stateLayout4:
                p = 3;
                break;
            case R.id.stateLayout5:
                p = 4;
                break;
            case R.id.stateLayout6:
                p = 5;
                break;
        }

//        T.showToast(getContext(), "我被点击了 " + p);

        if (p < 0) {
            return;
        }

        int state = ((LoadStateLayout) v).getState();
        VideoView videoView = videoViewList.get(p);
        ControlModel model = (ControlModel) videoView.getTag();
        switch (state) {
            case LoadStateLayout.STATE_SUCCESS://播放成功
                if (null != model) {
                    DataTemp.init().cameraInfo = model.cameraInfo;
                    BigLiveActivity.actionStart(getContext());
                }
                break;
            case LoadStateLayout.STATE_ERROR://播放失败,重新连接
                if (null != model) {
                    requestData(model.cameraInfo, videoView, p);

//                    DataTemp.init().cameraInfo = model.cameraInfo;
//                    BigLiveActivity.actionStart(getContext());
                }
                break;
        }
    }
}















